//
//  InfoListViewController.m
//  Utils
//
//  Created by enghou on 2018/6/4.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "InfoListViewController.h"
#import "NetWorkerManager.h"
#import "InfoItemModel.h"
#import <objc/runtime.h>
#import "SharingDetailViewController.h"
#import "MSSBrowseLocalViewController.h"
#import "VideoViewController.h"
@interface InfoListViewController ()<ShowMedia>

@end

@implementation InfoListViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        self.modelClsName = @"InfoItemModel";
        self.cellClass = @"InfoShareCellTableViewCell";
        self.path = @"/mobile/comprehensiveGovernance/sharedDocuments/getSharedDocumentsList.action";
    }
    return self;
}

-(NSDictionary *)param{
    if (!self.directoryId) {
        return nil;
    }
    return @{@"directoryId":self.directoryId};
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    InfoItemModel *model = self.source[indexPath.row];
    InfoItemModel *target = [[InfoItemModel alloc]init];
    u_int count;
    Ivar *ivars = class_copyIvarList([model class], &count);
    for (int i = 0; i < count; i++) {
        Ivar property = ivars[i];
        const char * name = ivar_getName(property);
        id value = object_getIvar(model, property);
        const char *memberType = ivar_getTypeEncoding(property);
        Ivar ivar = class_getInstanceVariable([InfoItemModel class], name);
        NSString *typeStr = [NSString stringWithCString:memberType encoding:NSUTF8StringEncoding];
        if ([typeStr isEqualToString:@"@\"NSString\""]) {
            //修改值
            object_setIvar(target, ivar, value);
        }else{
            object_setIvar(target, ivar, value);
        }
    }
    CGSize size = [model.content caculateSizeWithFont:[UIFont systemFontOfSize:14] MaxSize:CGSizeMake(ScreenWidth - 40, CGFLOAT_MAX)];
    target.height = model.height + size.height + 10;
    SharingDetailViewController *detail = [[SharingDetailViewController alloc]init];
    detail.title = @"详情";
    detail.model = target;
    [self push:detail];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    if ([cell respondsToSelector:@selector(setTheDelegate:)]) {
        [cell performSelector:@selector(setTheDelegate:) withObject:self];
    }
}

-(void)showVideoWithURL:(NSURL *)url{
    VideoViewController *video = [[VideoViewController alloc]init];
    video.url = url;
    [self presentViewController:video animated:YES completion:nil];
}

-(void)showAudioWithURL:(NSURL *)url{
    VideoViewController *video = [[VideoViewController alloc]init];
    video.url = url;
    [self presentViewController:video animated:YES completion:nil];
}

-(void)showFileWithURL:(NSURL *)url{
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *text = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:text preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sure];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)showImageWithImage:(NSArray *)image currentIndex:(NSInteger)idx sourceView:(UIImageView *)sourceView{
    NSMutableArray *images = [NSMutableArray array];
    for (UIImage *img in image) {
        MSSBrowseModel *model = [[MSSBrowseModel alloc]init];
        model.bigImage = img;
        model.smallImageView = sourceView;
        [images addObject:model];
    }
    MSSBrowseLocalViewController *local = [[MSSBrowseLocalViewController alloc]initWithBrowseItemArray:images currentIndex:idx];
    [local showBrowseViewController];
}


@end
